import os
import json
import datetime
import time


def get_json_files():
    """获取当前目录下的所有json文件"""
    files = [f for f in os.listdir(".") if f.endswith(".json")]
    return files


def parse_json_file(filename):
    """解析json文件"""
    try:
        with open(filename, "r", encoding="utf-8") as f:
            data = json.load(f)
        return data
    except Exception as e:
        print(f"解析 {filename} 时出错:", str(e))
        return None


def main():
    """主函数"""
    json_files = get_json_files()

    for file in json_files:
        data = parse_json_file(file)
        if data:
            # 获取列定义
            datas = data.get("datas", [])
            timeStr = "涨停明细数据[" + datetime.datetime.now().strftime("%Y%m%d") + "]"
            # timeStr = "涨停明细数据[20241209]"
            print(timeStr)
            # 处理涨停明细数据
            for item in datas:
                if 1:

                    first_limit_up = json.loads(item[timeStr])[0]
                    item["首次涨停时间"] = time.strftime(
                        "%Y-%m-%d %H:%M:%S",
                        time.localtime(first_limit_up["time"] / 1000),
                    )
                    item["首次涨停量"] = first_limit_up["firstVol"]
                    item["最高涨停量"] = first_limit_up["highestVol"]
                    item["最高涨停金额"] = round(
                        float(first_limit_up["highestVol"])
                        * float(item["最新价"])
                        / 100000000,
                        2,
                    )
                    # 删除原始timeStr数据
                    del item[timeStr]


if __name__ == "__main__":
    main()
